package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 护理计划Service业务层处理
 *
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //先根据id查询计划的基本信息
        NursingPlan nursingPlan = this.getById(id);
        //根据计划id查询所包含的项目,查询中间表
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingProjectPlan::getPlanId,id);
        List<NursingProjectPlan> nursingProjectPlans = iNursingProjectPlanService.list(queryWrapper);
        //封装数据
        NursingPlanVo nursingPlanVo = BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> projectPlanVos = nursingProjectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.copyProperties(projectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).toList();
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //将前端传过来的基本数据传给计划对象
        NursingPlan nursingPlan = BeanUtil.copyProperties(dto, NursingPlan.class);
        //执行新增计划
        this.save(nursingPlan);
        //执行新增计划中包含的项目
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());

        });
        iNursingProjectPlanService.saveBatch(projectPlans);
        return 1 ;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //赋值
        NursingPlan nursingPlan = BeanUtil.copyProperties(dto, NursingPlan.class);
        //先把计划的基本信息修改
        this.updateById(nursingPlan);
        //根据计划id删除对应的护理项目
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingProjectPlan::getPlanId,dto.getId());
        iNursingProjectPlanService.remove(queryWrapper);
        //判断护理项目是否为空
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)){
            //不为空,将数据上传到数据库
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        iNursingProjectPlanService.saveBatch(projectPlans);
        }
        return 1;
    }
    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        //先根据计划id删除中间表
        iNursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId,id));
        //在删除计划基本信息
        this.removeById(id);
        return 1;
    }

    /**
     * 查询所有的护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> selectAll() {
        return this.list();
    }
}
